package com.fdb.efp.esb.service.impl.tranditionalcore;

import com.fdb.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.fdb.efp.esb.service.facade.tranditionalcore.PeriSystemStatusUpdateService;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.efp.esb.service.bo.req.traditionalcore.PeriSystemStatusUpdateReqBean;
import com.fdb.efp.esb.service.bo.resp.traditionalcore.PeriSystemStatusUpdateRespBean;
import com.fdb.efp.esb.service.client.ESBClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 外围系统状态请求: <br>
 * <p>
 * 0.1:caoxin:2019/6/20 16:44: 外围系统状态请求: <br>
 *
 * @author caoxin
 * @version 0.1
 * @date 2019/6/20 16:44
 * @since 2019/6/20 16:44
 */
@Service("periSystemStatusUpdateService")
public class PeriSystemStatusUpdateServiceImpl implements PeriSystemStatusUpdateService {
    /**
     * 日志信息
     */
    private final Logger logger = LoggerFactory.getLogger(PeriSystemStatusUpdateServiceImpl.class);

    @Override
    public PeriSystemStatusUpdateRespBean updateTellerStatus(PeriSystemStatusUpdateReqBean periSystemStatusUpdateReqBean) throws Exception {
        // 请求外围系统状态更新
        EsbRespServiceBean esbRespServiceBean = ESBClientFactory.
                buildClient().
                withTradeNo(ESBConstant.SYSTEM_ID_TRANTIONAL_CORE).
                withScene(ESBConstant.SCENE_NO_01).
                withBody(periSystemStatusUpdateReqBean).
                create().
                sendAndReceive();
        // 返回对象
        PeriSystemStatusUpdateRespBean periSystemStatusUpdateRespBean = null;
        // 判断是否成功
        Optional<EsbRespSysHeadBean> sysHead = Optional.ofNullable(esbRespServiceBean.getSysHead());
        if (sysHead.isPresent()) {
            Optional<EsbRespRetInfBean> esbRespRetInfBean = sysHead.get().getRetInfArry().stream().findFirst();
            if (esbRespRetInfBean.isPresent()) {
                String retCd = esbRespRetInfBean.get().getRetCd();
                String retMsg = esbRespRetInfBean.get().getRetMsg();
                if (ESBConstant.SUCCESS.equals(retCd)) {
                    String msg = "++++====>调用外围系统状态更新接口成功<====++++";
                    logger.debug(msg);
                    periSystemStatusUpdateRespBean = esbRespServiceBean.getBody(PeriSystemStatusUpdateRespBean.class);
                } else {
                    String msg = "++++====>调用外围系统状态更新失败<====++++ " + retMsg;
                    logger.error(msg);
                    throw new ESBException(msg);
                }
            }

        }
        return periSystemStatusUpdateRespBean;
    }
}
